using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TicTacToe
{
    public class TicTacToeGame : Game
    {
        public Player[] players { get; set; }
        public TicTacToeGrid ourGrid { get; set; }
        public int turn { get; set; }

        // Initialize a new game, only run when at the begining of a game
        public override void Initialize()
        {
            players = new Player[2];
            players[0] = new Player("X", 1, true );
            players[1] = new Player("O", 2, false);
            turn = new int();
            turn = 0;
            ourGrid = new TicTacToeGrid();
            ourGrid.ClearGrid();
        }

        //Basically checks the status of a current game, if a player has one or if there is an tie.
        // returning 0 means there is an winner.
        // returning 2 means there is an tie.
        // returning 3 means the game must go on.
        public override int CheckStatus()
        {
  
            //Checks the wining positions, if found then it returns 0
            if (ourGrid.gameGrid[0] != 0 && ourGrid.gameGrid[0] == ourGrid.gameGrid[1] && ourGrid.gameGrid[1] == ourGrid.gameGrid[2] && NCover.NCoverCheck.Add(8))
                return 0;
            if (ourGrid.gameGrid[3] != 0 && ourGrid.gameGrid[3] == ourGrid.gameGrid[4] && ourGrid.gameGrid[4] == ourGrid.gameGrid[5] && NCover.NCoverCheck.Add(9))
                return 0;
            if (ourGrid.gameGrid[6] != 0 && ourGrid.gameGrid[6] == ourGrid.gameGrid[7] && ourGrid.gameGrid[7] == ourGrid.gameGrid[8] && NCover.NCoverCheck.Add(10))
                return 0;
            if (ourGrid.gameGrid[0] != 0 && ourGrid.gameGrid[0] == ourGrid.gameGrid[3] && ourGrid.gameGrid[3] == ourGrid.gameGrid[6] && NCover.NCoverCheck.Add(11))
                return 0;
            if (ourGrid.gameGrid[1] != 0 && ourGrid.gameGrid[1] == ourGrid.gameGrid[4] && ourGrid.gameGrid[4] == ourGrid.gameGrid[7] && NCover.NCoverCheck.Add(12))
                return 0;
            if (ourGrid.gameGrid[2] != 0 && ourGrid.gameGrid[2] == ourGrid.gameGrid[5] && ourGrid.gameGrid[5] == ourGrid.gameGrid[8] && NCover.NCoverCheck.Add(13))
                return 0;
            if (ourGrid.gameGrid[0] != 0 && ourGrid.gameGrid[0] == ourGrid.gameGrid[4] && ourGrid.gameGrid[4] == ourGrid.gameGrid[8] && NCover.NCoverCheck.Add(14))
                return 0;
            if (ourGrid.gameGrid[2] != 0 && ourGrid.gameGrid[2] == ourGrid.gameGrid[4] && ourGrid.gameGrid[4] == ourGrid.gameGrid[6] && NCover.NCoverCheck.Add(15))
                return 0;
            for (int i = 0; i < ourGrid.gameGrid.Length && NCover.NCoverCheck.Add(16); i++)
            {
                //Checks the whole array, it returns 3 if everything is ok
                if (ourGrid.gameGrid[i] == 0 && NCover.NCoverCheck.Add(17))
                    return 3;
            }
            //returning 2 means there is an Tie
            return 2;
        }
        //Make sure you are doing a legit move, if not then it returns false
        public override bool CheckMove(int pos)
        {
            //Checks if you can place at a certain position or not
            if (ourGrid.gameGrid[pos] == 0 && NCover.NCoverCheck.Add(18))
                return true;
            NCover.NCoverCheck.Add(7); return false;
        }
        //This function is called at an end of round, it sets which player will
        //start the round, depending on wether he started in the previous round.
        public override void reset()
        {
            ourGrid.ClearGrid();
            if (players[0].starts == true && NCover.NCoverCheck.Add(19))
            {
                players[0].starts = false;
                players[1].starts = true;
                turn = 1;
            }
            else
            {
                players[0].starts = true;
                players[1].starts = false;
                turn = 0;
            }
        }

    }
}

